/************************************************
 * Author       :   XXY
 * Blog         :   https://www.cnblogs.com/jzyl
 * Gitee        :   https://gitee.com/xiaobaichong
 * Email        :   1047185209@QQ.com
 * FileName     :   MenuTools.cs
 * CreateData   :   2023/5/25 9:48:34
 * UnityVersion :   2021.3.20f1c1
 * Description  :   菜单工具集
************************************************/

using Sirenix.OdinInspector.Editor;
using Sirenix.Utilities;
using Sirenix.Utilities.Editor;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using Main;
using Sirenix.OdinInspector;
using System.Linq;

public class MenuTools : OdinMenuEditorWindow
{
    [MenuItem("Tools/My Tools")]
    private static void OpenWindow()
    {
        var window = GetWindow<MenuTools>();
        window.titleContent = new GUIContent("My Tools");
        window.position = GUIHelper.GetEditorWindowRect().AlignCenter(1280, 720);
    }


    protected override OdinMenuTree BuildMenuTree()
    {
        var tree = new OdinMenuTree(true);

        var customMenuStyle = new OdinMenuStyle
        {
            BorderPadding = 0f,
            AlignTriangleLeft = true,
            TriangleSize = 16f,
            TrianglePadding = 0f,
            Offset = 20f,
            Height = 23,
            IconPadding = 0f,
            BorderAlpha = 0.323f
        };
        tree.DefaultMenuStyle = customMenuStyle;

        tree.Config.DrawSearchToolbar = true;

        #region 介绍
        tree.AddObjectAtPath("介绍", new MenuToosInfo());
        #endregion

        #region 数据
        tree.AddObjectAtPath("数据", new ConfigHelperInfo());
        tree.AddObjectAtPath("数据/Color Config", CreateScriptableObject.GetScriptableObject<ColorConfig, ColorInfo>(PathConfig.ScriptableObjectPath + "ConfigData/" + nameof(ColorConfig) + ".asset"));
        tree.AddObjectAtPath("数据/Sprite Config", CreateScriptableObject.GetScriptableObject<SpriteConfig, SpriteInfo>(PathConfig.ScriptableObjectPath + "ConfigData/" + nameof(SpriteConfig) + ".asset"));
        tree.AddObjectAtPath("数据/Audio Config", CreateScriptableObject.GetScriptableObject<AudioConfig, AudioClip>(PathConfig.ScriptableObjectPath + "ConfigData/" + nameof(AudioConfig) + ".asset"));
        tree.AddObjectAtPath("数据/Prefab Config", CreateScriptableObject.GetScriptableObject<PrefabConfig, PrefabInfo>(PathConfig.ScriptableObjectPath + "ConfigData/" + nameof(PrefabConfig) + ".asset"));
        tree.AddObjectAtPath("数据/Video Config", CreateScriptableObject.GetScriptableObject<VideoConfig, VideoInfo>(PathConfig.ScriptableObjectPath + "ConfigData/" + nameof(VideoConfig) + ".asset"));
        tree.AddObjectAtPath("数据/Text Config", CreateScriptableObject.GetScriptableObject<TextConfig, string>(PathConfig.ScriptableObjectPath + "ConfigData/" + nameof(TextConfig) + ".asset"));
        tree.AddObjectAtPath("数据/Texture Config", CreateScriptableObject.GetScriptableObject<TextureConfig, TextureInfo>(PathConfig.ScriptableObjectPath + "ConfigData/" + nameof(TextureConfig) + ".asset"));
        #endregion


        #region 框架组件
        tree.AddObjectAtPath("框架", new FrameworkHelperInfo());
        tree.AddObjectAtPath("框架/组件", AssetDatabase.LoadAssetAtPath<FrameworkConfig>(PathConfig.ScriptableObjectPath + "ConfigFramework/" + "ConfigFramework.asset"));
        #endregion


        #region 工具
        tree.AddObjectAtPath("工具", new EditorToolInfo());
        tree.AddObjectAtPath("工具/Build Asset Bundle", new BuildAssetBundle());
        tree.AddObjectAtPath("工具/Script Head Window", new ScriptHeadWindow());
        tree.AddObjectAtPath("工具/Code Merge Window", new CodeMergeWindow());
        tree.AddObjectAtPath("工具/Online Toolbox", new OnlineToolbox());
        tree.AddObjectAtPath("工具/Player Prefs Viewer", new PlayerPrefsViewer());
        #endregion


        #region Settings   
        tree.AddObjectAtPath("Settings", new SettingsInfo());
        tree.AddObjectAtPath("Settings/Player Settings", Resources.FindObjectsOfTypeAll<PlayerSettings>().FirstOrDefault());
        tree.Add("Settings/Framework Settings", new FrameworkSetting(), EditorGUIUtility.IconContent("icon_api.png").image);
        #endregion

        tree.EnumerateTree().AddThumbnailIcons().SortMenuItemsByName();

        return tree;
    }

    protected override void OnDestroy()
    {
        base.OnDestroy();
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
    }

    /// <summary>
    /// MenuToos 介绍
    /// </summary>
    public class MenuToosInfo
    {
        [Title("介绍")]
        [HideLabel, TextArea(10, 40)]
        public string info = "欢迎使用！\n这是一款功能集合，提高开发效率。";
    }
    /// <summary>
    /// ConfigHelper 介绍
    /// </summary>
    private class ConfigHelperInfo
    {
        [Title("介绍")]
        [HideLabel, TextArea(10, 60)]
        public string info = "部分数据是在Excel配置生成的，数据是依附在ConfigData预制件上的，该预制件通过AB包加载出来的。\n\n在编辑器下可直接使用:\n\nConfigDataHelper.Get<T>()获取数据。\n\n在非编辑器下:\n\n首先要下载ConfigData AB包并通过AssetBundlesManager.Instance.ABLocalPath设置AB包所在路径，再通过ConfigDataHelper.Get<T>()获取数据。";
    }
    /// <summary>
    /// FrameworkConfig 介绍
    /// </summary>
    private class FrameworkHelperInfo
    {
        [Title("介绍")]
        [HideLabel, TextArea(10, 60)]
        public string info = "框架通过预制件生成，获取得到的是预制件实例化对象，可通过获取其节点上的脚本或者使用热更脚步驱动框架逻辑。\n\n在编辑器下可通过使用：\n\nConfigFrameworkHelper.Get<T>(name)获取预制件实例化。\n\n在非编辑器下：\n\n首先要下载ConfigFramework AB包并通过AssetBundlesManager.Instance.ABLocalPath设置AB包所在路径，再使用ConfigFrameworkHelper.Get<T>(name)获取预制件实例化。";
    }
    /// <summary>
    /// 编辑器下使用的工具
    /// </summary>
    private class EditorToolInfo
    {
        [Title("介绍")]
        [HideLabel, TextArea(10, 40)]
        public string info = "该工具只在编辑器下使用";
    }
    /// <summary>
    /// 编辑器下使用的工具
    /// </summary>
    private class SettingsInfo
    {
        [Title("介绍")]
        [HideLabel, TextArea(10, 40)]
        public string info = "项目设置";
    }
}
